package com.tiandang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tiandang.core.error.BusinessException;
import tiandang.core.error.EmBusinessError;
import tiandang.core.model.Manager;
import tiandang.core.model.Promo;
import tiandang.core.model.PromoManager;
import tiandang.core.model.PromoProduct;
import tiandang.core.response.CommonReturnType;
import tiandang.core.service.*;

import java.util.*;

@RestController
@RequestMapping("/api/promo")
public class PromoController {

  @Autowired
  private PromoService promoService;

  @Autowired
  private PromoManagerService promoManagerService;

  @Autowired
  private ManagerService managerService;

  @Autowired
  private PromoProductService promoProductService;

  @Autowired
  private WXService wxService;

  @GetMapping("/getList")
  public CommonReturnType getList(@RequestParam(value = "startTimestamp", required = false) Long startTimestamp,
                                  @RequestParam(value = "endTimestamp", required = false) Long endTimestamp,
                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
    return CommonReturnType.success(promoService.getList(startTimestamp, endTimestamp, currentPage, limit));
  }

  @PostMapping("/add")
  public CommonReturnType add(@RequestBody Promo promo) throws BusinessException {
    if (promo.getPromoManagerList() == null || promo.getPromoManagerList().size() == 0) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, "开团团长不能为空");
    }

    if (promo.getPromoProductList() == null || promo.getPromoProductList().size() == 0) {
      throw new BusinessException(EmBusinessError.PARAMMETER_VALIDATION_ERROR, "开团商品不能为空");
    }

    promoService.save(promo);
    return CommonReturnType.success();
  }

  @PostMapping("/update")
  public Object update(@RequestBody Promo promo) throws BusinessException {
    promoService.update(promo);
    return CommonReturnType.success();
  }


  @PostMapping("/delete")
  public CommonReturnType delete(Integer id) {
    promoService.delete(id);
    return CommonReturnType.success();
  }

  @GetMapping("/getDetail")
  public CommonReturnType getDetail(Integer id) {
    Map<String, Object> resultMap = new HashMap<>();
    Promo promo = promoService.getPromoById(id);
    Long now = new Date().getTime();

    if (promo.getStartTimestamp() > now) {
      promo.setStatus(0); //0未开始
    } else if (promo.getEndTimestamp() < now) {
      promo.setStatus(2); //2已结束
    } else {
      promo.setStatus(1); //1进行中
    }

    //获取promoManagerList
    List<PromoManager> promoManagerList = promoManagerService.getListByPromoId(id);
    List<Integer> managerIdList = new ArrayList<>();
    for (PromoManager promoManager : promoManagerList) {
      managerIdList.add(promoManager.getManagerId());
    }

    List<Manager> managerList = new ArrayList<>();
    if (managerIdList.size() > 0) {
      managerList = managerService.getListByIdList(managerIdList);
    }

    //获取promoProductList
    List<PromoProduct> promoProductList = promoProductService.getListByPromoId(id);

    resultMap.put("promo", promo);
    resultMap.put("managerList", managerList);
    resultMap.put("productList", promoProductList);
    return CommonReturnType.success(resultMap);
  }

  // 隐藏/显示
  @PostMapping("/setIsShow")
  public CommonReturnType setIsShow(@RequestParam(value = "id") Integer id, @RequestParam(value = "isShow") int isShow) {
    promoService.setIsShow(id, isShow);
    return CommonReturnType.success();
  }

  //现在结束活动
  @PostMapping("/overNow")
  public CommonReturnType overNow(@RequestParam(value = "id") Integer id) {
    promoService.overNow(id);
    return CommonReturnType.success();
  }

  //给用户推送开团消息
  @PostMapping("/pushMessage")
  public CommonReturnType pushMessage(@RequestParam(value = "id") Integer id) throws BusinessException {
    wxService.pushMessage(id);
    return CommonReturnType.success();
  }

  // 修改排序
  @PostMapping("/updateSequence")
  public CommonReturnType updateSequence(@RequestParam(value = "id") Integer id,
                               @RequestParam(value = "sequence") Integer sequence) {
    promoProductService.updateSequenceById(id, sequence);
    return CommonReturnType.success();
  }
}
